package com.lm.scau.dyb.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lm.scau.dyb.R;
import com.lm.scau.dyb.adapter.MovieAdapter;
import com.lm.scau.dyb.bean.Movie;
import com.lm.scau.dyb.db.Dalx.MovieDalx;
import com.lm.scau.dyb.fragment.TitleFragment;
import com.lm.scau.dyb.inter.BasicUIEvent;
import com.lm.scau.dyb.inter.UICore;

import java.util.ArrayList;
import java.util.List;

/**
 * Function:
 * auth：liming on 2016/8/31 15:49
 * mail：liming@wxchina.com
 */
public class MoviesActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener, BasicUIEvent, Handler.Callback {
    private final static int pageSize = 2;

    private final static int MSG_GET_MOVIES = 10;
    private TitleFragment titleBar;
    private PullToRefreshListView pullToRefreshView;
    private String type;
    private String role;

    private boolean isRefleshing = false;
    private List<Movie> movieList;

    private MovieAdapter adapter;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);
        Bundle bundle = getIntent().getExtras();
        type = bundle.getString("type");
        role = bundle.getString("role");
        movieList = new ArrayList<>();
        handler = new Handler(this);
        initView();
        adapter = new MovieAdapter(this, movieList);
        pullToRefreshView.setAdapter(adapter);
        UICore.eventTask(this, this, MSG_GET_MOVIES, null, -1, true);
    }

    private void initView() {
        titleBar = (TitleFragment) getSupportFragmentManager().findFragmentById(R.id.title_fragment);
        titleBar.initTitle(false, true, role + "电影");
        pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
        pullToRefreshView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        pullToRefreshView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载");
        pullToRefreshView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载");
        pullToRefreshView.getLoadingLayoutProxy(false, true).setReleaseLabel("放开以加载");
        pullToRefreshView.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        if (pullToRefreshView.isFooterShown() && !isRefleshing) {
            isRefleshing = true;
            int rowId = adapter.getLastRowId();
            UICore.eventTask(this, this, MSG_GET_MOVIES, null, rowId, false);
        } else {
            pullToRefreshView.onRefreshComplete();
        }
    }

    @Override
    public void execute(int msg, Object obj) {
        switch (msg) {
            case MSG_GET_MOVIES:
                getMoviesFromLocal(type, obj, role);
                break;
            default:
                break;
        }
    }

    private void getMoviesFromLocal(String type, Object obj, String role) {
        int rowId = (int) obj;
        MovieDalx dalx = new MovieDalx(this);
        List<Movie> list = dalx.getMovies(role, type, rowId, pageSize);
        Message msg = Message.obtain(handler, MSG_GET_MOVIES, list);
        msg.sendToTarget();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_GET_MOVIES:
                dealResult(msg.obj);
                break;
            default:
                break;
        }
        return false;
    }

    private void dealResult(Object obj) {
        isRefleshing = false;
        pullToRefreshView.onRefreshComplete();
        List<Movie> list = (List<Movie>) obj;
        if (list.size() == 0) {
            return;
        }
        movieList.addAll(list);
        //   movieList.notify();
    }
}
